#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

static void *thread_start(void *arg)
{
    int i, s;
    pthread_attr_t gattr;
    s = pthread_getattr_np(pthread_self(), &gattr);
    s = pthread_attr_getdetachstate(&gattr, &i);
    printf("detach state = %s\n",
           (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREAT_DETACHED" : (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" : "???");

    pthread_detach(pthread_self());
    
    s = pthread_getattr_np(pthread_self(), &gattr);
    s = pthread_attr_getdetachstate(&gattr, &i);
    printf("after detach\n");
    printf("detach state = %s\n",
           (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREAT_DETACHED" : (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" : "???");
    pthread_attr_destroy(&gattr);
}

int main()
{
    pthread_t tid;
    int s;
    s = pthread_create(&tid, NULL, &thread_start, NULL);

    pthread_exit(NULL);
}